import router from "@/router"
import { ref} from "vue"
import * as api from "@/api/permission.js"
import {useBreadCrumb} from "@/store/breadCrumb.js"
import {usePermission} from "@/store/permission.js"
import NProgress from "nprogress"
import 'nprogress/nprogress.css'
// let breadCrumbStore = breadCrumbUser()

// 前置钩子
router.beforeEach(async (to,from)=>{
    NProgress.start()
    let permissionStore = usePermission()



    //判断本地的sessionStorage中是否有token，如果有，啥也不干
    let token = sessionStorage.getItem('myx-token')
    if(!token){
        //如果没有
        //1。访问的不是登入页，强行跳转到登入页
        
        if(to.path !== '/login'){
            return {
                path:'/login'
            }
        }
    }else{
        // 如果menulist的长度是0，那么就要发起请求
        if(permissionStore.menulist.length===0){
            // 调用store中的action方法发起异步请求
            await permissionStore.fetch_menulist()
            // 废弃要进入的路由，重新进入动态添加的路由
            return {
                ...to
            }
        }
        

    }

    //2.访问的是登入页，直接放行


    
})




// 后置钩子
router.afterEach((to,from)=>{
    NProgress.done()
   
    // console.log('werhghjadsfg',to.matched)
    let breadCrumbStore = useBreadCrumb()
     
    breadCrumbStore.$patch((state)=>{
        state.bread = to.matched
    }) 
    // console.log('132546789',bread.value)
    
    

})